package com.share.example.spider.processor;

import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.share.common.utils.DateUtil;
import com.share.example.spider.model.db.StockDay;
import com.share.example.spider.utils.DriverUtil;
import com.share.example.spider.utils.WebClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/10/28 15:26
 */
@Slf4j
public class StockDayProcessor implements PageProcessor {

    /**
     * 1.解析数据
     * 2.获取下一页的链接
     * 3.通过page把数据推出去
     */
    @Override
    public void process(Page page) {
        String url = page.getUrl().get();
//        StockDay stockDay = htmlUnit(url);
        StockDay stockDay = seleniumHtml(page);

        stockDay.setCreateTime(new Date());
        stockDay.setOneDate(DateUtil.getDate());

        page.putField(StockDay.class.getName(), stockDay);
    }

    /*site的作用是限定爬取的网络位置*/
    @Override
    public Site getSite() {
        Site site = new Site();
        site.setDomain("quote.eastmoney.com");
        site.setUserAgent("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36");
        return site;
    }

    public static void main(String[] args) {
        WebDriver driver = DriverUtil.getDriver(StockDayProcessor.class.getName() + "-" + Thread.currentThread().getName());
        driver.get("http://quote.eastmoney.com/sz301049.html");
        String gt1 = driver.findElement(By.id("gt1")).getText();
        System.out.println(gt1);
    }

    private StockDay seleniumHtml(Page page) {
        WebDriver driver = DriverUtil.getDriver(StockDayProcessor.class.getName() + "-" + Thread.currentThread().getName());
        driver.get(page.getUrl().get());

        String code = driver.findElement(By.id("code")).getText();
        String name = driver.findElement(By.id("name")).getText();

        String startPrice = driver.findElement(By.id("gt1")).getText();
        String endPrice = driver.findElement(By.id("gt8")).getText();
        String topPrice = driver.findElement(By.id("gt2")).getText();
        String lowPrice = driver.findElement(By.id("gt9")).getText();

        StockDay stockDay = new StockDay();
        stockDay.setStartPrice(new Double(startPrice));
        stockDay.setEndPrice(new Double(endPrice));
        stockDay.setTopPrice(new Double(topPrice));
        stockDay.setLowPrice(new Double(lowPrice));
        stockDay.setCode(code);
        stockDay.setName(name);
        return stockDay;
    }

    private StockDay htmlUnit(String url) {
        HtmlPage htmlPage = null;
        StockDay stockDay = new StockDay();
        try {
            String name = this.getClass().getName() + "-" + Thread.currentThread().getName();
            htmlPage = WebClientUtil.getWebClient(name).getPage(url);//将客户端获取的树形结构转化为HtmlPage
            Thread.sleep(3000);//主线程休眠10秒 让客户端有时间执行js代码
        } catch (Exception e) {
            e.printStackTrace();
        }

        DomElement tbody = htmlPage.getElementById("quote-digest").getFirstElementChild().getFirstElementChild();

        DomNodeList<HtmlElement> tr = tbody.getElementsByTagName("tr");
        for (HtmlElement ht : tr) {
            DomNodeList<HtmlElement> td = ht.getElementsByTagName("td");
            List<String> tvalue = td.stream().map(e -> e.getTextContent()).collect(Collectors.toList());

            int len = tvalue.size();
            if (len % 2 != 0 || len <= 0) {
                continue;
            }

            for (int i = 0; i < len; i++) {
                String name = tvalue.get(i);
                if (name.contains("涨停")) {
                    stockDay.setTopPrice(new Double(tvalue.get(i + 1)));
                } else if (name.contains("最低")) {
                    stockDay.setLowPrice(new Double(tvalue.get(i + 1)));
                } else if (name.contains("今开")) {
                    stockDay.setStartPrice(new Double(tvalue.get(i + 1)));
                } else if (name.contains("昨收")) {
                    stockDay.setEndPrice(new Double(tvalue.get(i + 1)));
                }
            }
        }
        return stockDay;
    }
}
